package com.rainman.simpleredis.bootstart.configuration;

import com.rainman.simpleredis.core.SimpleRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;

import java.nio.charset.Charset;


/**
 * 监听redis发布订阅消息
 *
 */
public class RedisSubListener implements MessageListener {

    @Autowired
    SimpleRedis simpleRedis;

    RedisSubListener(SimpleRedis simpleRedis){
        this.simpleRedis=simpleRedis;
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {

        String key = new String(message.getBody(), Charset.forName( simpleRedis.getSimpleConfig().getCharset()));

        simpleRedis.dealSubTig(key);

    }

}
